package com.project.oauth2;

import com.alibaba.fastjson2.JSON;
import com.project.entity.OAuth2ClientDetailsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.*;
import java.util.stream.Collectors;

public class ClientDetails implements org.springframework.security.oauth2.provider.ClientDetails {

    private static final Logger log = LoggerFactory.getLogger(ClientDetails.class);

    private OAuth2ClientDetailsEntity details;

    public ClientDetails(OAuth2ClientDetailsEntity details) {
        this.details = details;
    }

    @Override
    public String getClientId() {
        return details.getClientID();
    }

    @Override
    public Set<String> getResourceIds() {
        return new HashSet<>(Arrays.asList(details.getResourceIds().split(",")));
    }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    @Override
    public String getClientSecret() {
        return details.getClientSecret();
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    @Override
    public Set<String> getScope() {
        return new HashSet<>(Arrays.asList(details.getScope().split(",")));
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return new HashSet<>(Arrays.asList(details.getAuthorizedGrantTypes().split(",")));
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return new HashSet<>(Arrays.asList(details.getWebServerRedirectUri().split(",")));
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        List<String> strings = Arrays.asList(details.getAuthorities().split(","));
        List<GrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        for (String string : strings) {
            SimpleGrantedAuthority simpleGrantedAuthority
                    = new SimpleGrantedAuthority(string);
            simpleGrantedAuthorities.add(simpleGrantedAuthority);
        }
        return simpleGrantedAuthorities;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return details.getAccessTokenValidity();
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return details.getRefreshTokenValidity();
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }

}
